<template>
  <div class="login-container">
    <el-form ref="form" :model="form" label-width="80px" class="login-form">
      <div class="login-title">系统登录</div>

      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>

      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item class="form-actions">
        <el-button type="primary" @click="onLogin" class="login-btn">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api/user";
export default {
  name: "LoginView",
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    onLogin() {
      login(this.form).then(res => {
        if (res.code === 200) {
          this.$message.success("登录成功");
          sessionStorage.setItem("username", res.data.username);
          this.$router.push({ path: '/HomeView' });
        } else {
          this.$message.error("用户名或密码错误");
        }
      });
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-form {
  width: 100%;
  max-width: 400px;
  background-color: #fff;
  padding: 30px 40px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.login-form:hover {
  transform: translateY(-5px);
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 30px;
  position: relative;
}

.login-title:after {
  content: '';
  display: block;
  width: 50px;
  height: 3px;
  background: #667eea;
  margin: 10px auto 0;
  border-radius: 3px;
}

.el-input {
  width: 100%;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.el-input__inner {
  height: 42px;
  border-radius: 4px;
  border-color: #ddd;
  transition: all 0.3s ease;
}

.el-input__inner:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

/* 按钮容器样式修改，确保按钮居中 */
.form-actions {
  margin-top: 20px;
  text-align: center; /* 文本居中 */
}

.login-btn {
  margin-left: -55px;
  width: 100%;
  max-width: 200px; /* 限制最大宽度，使其看起来更美观 */
  height: 42px;
  font-size: 16px;
  background: #667eea;
  border-color: #667eea;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.login-btn:hover {
  background: #5a6fda;
  border-color: #5a6fda;
  transform: translateY(-2px);
}

.login-btn:active {
  transform: translateY(0);
}

.el-form-item__label {
  font-weight: 500;
  color: #555;
}

.el-form-item {
  margin-bottom: 20px;
  transition: all 0.3s ease;
}
</style>
